package cn.zhangjie.basic.service.imp;

import cn.zhangjie.basic.mapper.BasicMapper;
import cn.zhangjie.basic.query.BaseQuery;
import cn.zhangjie.basic.service.IBasicService;
import cn.zhangjie.basic.util.PageList;
import cn.zhangjie.order.dto.OrderDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public abstract class BasicServiceImpl<T> implements IBasicService<T> {
    
    @Autowired
    private BasicMapper<T> mapper;

    @Transactional//默认只读为false，默认传播机制为REQUIRED[有且仅有一个事务]
    @Override
    public void add(T t) {
        mapper.save(t);
    }

    @Transactional
    @Override
    public void del(Long id) {
        mapper.remove(id);
    }

    @Transactional
    @Override
    public void update(T t) {
        mapper.update(t);
    }

    @Override
    public T findById(Long id) {
        return (T) mapper.loadById(id);
    }

    @Override
    public List<T> findAll() {
        return mapper.loadAll();
    }

    @Override
    public PageList<T> queryPage(BaseQuery query) {
        Long count = mapper.queryCount(query);
        if(count < 1){
            return new PageList<T>();
        }

        List<T> list = mapper.queryData(query);

        return new PageList<T>(count,list);
    }

    @Override
    public void pactchdel(List<Long> ids) {
        mapper.pactchdel(ids);
    }

    
}
